--
-- Author: shunguo.chen
-- Date: 2018-06-27 20:05:17
--
-- BlessView
--

local Armature = app:getClass("Armature")
local BlessRender = import(".renders.BlessRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.BlessScene

local BLESS_NUM_STR = L("lua_code_text_121")

function ClassRef:init()
	local panel = self._ui.Panel
	display.uiAddClick(panel, function()
			executeFunc(self._onCloseView, self)
		end)

	self._ui.Button_close_0:addClickEventListener(function()
			executeFunc(self._onCloseView, self)
		end)

	local renders = {0,0,0}
	for i=1,3 do
		local render = BlessRender.create(panel:getChildByName("Panel_" .. i))
		render:onBtnClick(handler(self, self._onClickRender))
		renders[i] = render
	end
	self._renders = renders

	self._tltText = self._ui["Panel/Panel_tlt/name"]
	self._numText = self._ui["Panel/Panel_bom/tips"]
end

function ClassRef:_onClickRender(render)
	executeFunc(self._onOperate, "openBlessReward", render:getData())
end

-- mapInfo = MapInfo
function ClassRef:refresh(mapInfo)
	self._tltText:setString(mapInfo:getName())

	local zoneInfos = mapInfo:getZones()
	for i,render in ipairs(self._renders) do
		render:setData(zoneInfos[i])
	end
end

-- zoneInfo = ZoneInfo
function ClassRef:updateZone(zoneInfo)
	for i,render in ipairs(self._renders) do
		local data = render:getData()
		if data.id == zoneInfo.id then
			self:_playBlessAction(render)
			break
		end
	end
end

function ClassRef:_playBlessAction(render)
	local posX, posY = render:getPosition()

	-- init aniNode and timeline
	local aniNode = self._aniNode
	local timeline, aniParticle
	if not aniNode then
		aniNode = cc.CSLoader:createNode(Res.BlessAniNode, true)
		aniNode:setPositionY(posY)
		self._ui.Panel:addChild(aniNode, 2)
		aniNode:setVisible(false)
		timeline = aniNode:getTimeline()
		timeline:setLastFrameCallFunc(function(sender)
				aniNode:setVisible(false)
			end)
		aniParticle = aniNode:findChild("Panel/flash_01")
		self._aniNode = aniNode
	else
		timeline = aniNode:getTimeline()
		aniParticle = aniNode:findChild("Panel/flash_01")
	end

	-- init armature
	local armature = self._armature
	if not armature then
		local _, renderHei = render:getContentSize2()

		local params = {
			armatureName = "flyxin",
			path = "armatures/effect/flyxin"
		}
		armature = Armature.create(params)
		armature:setPositionY(posY + renderHei / 2)
		self._ui.Panel:addChild(armature, 2)
		armature:setVisible(false)
		self._armature = armature
	end

	-- play armature -> play timeline
	armature:setVisible(true)
	armature:setPositionX(posX)
	armature:play("flyxin", 1, function(sender)
			sender:setVisible(false)
		end)
	armature:onFrameEvent(function(sender, name)
			if name == "fire" then
				aniNode:setVisible(true)
				aniNode:setPositionX(posX)
				timeline:gotoFrameAndPlay(0, false)
				aniParticle:resetSystem()
			end
		end)

	-- Delay render:playAction
	render:performWithDelay(function()
			render:playAction()
		end, 0.5)
end

function ClassRef:refreshBlessTimes(blessTimes)
	self._numText:setString(string.format(BLESS_NUM_STR, blessTimes))
end

return ClassRef
